package com.unicom.projt.fee;

import android.content.Intent;
import com.unicom.projt.http.AbstractHttpController;
import com.unicom.projt.http.UriConstants;
import com.unicom.projt.util.*;

import java.text.DecimalFormat;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-17
 * Time: 下午5:41
 */
public class IvrController extends AbstractHttpController<String> {
    private static final String CHECK_NUMBER = "CHECK_NUMBER";
    private static final String CHECK_BIND = "CHECK_BIND";
    private static final String PAY = "PAY";

    private PaymentPage paymentPage;
    private PaymentConfirmPage confirmPage;
    private String step = "0";
    private Double amount;
    private String amountPhoneNumber;
    private String payPhoneNumber;
    private String actionType = "";

    public IvrController(PaymentPage paymentPage) {
        super(String.class);
        this.step = "0";
        this.paymentPage = paymentPage;
    }

    public IvrController(PaymentConfirmPage confirmPage) {
        super(String.class);
        this.confirmPage = confirmPage;
        this.step = "1";
    }

    public void checkThePaymentPhoneNumber(String phoneNumber, String amountPhoneNumber, Double amount) {
        this.payPhoneNumber = phoneNumber;
        this.amountPhoneNumber = amountPhoneNumber;
        this.amount = amount;
        this.actionType = CHECK_NUMBER;
        String checkPhoneNumberXml = new PostXmlBuilder().setBiPCode(RequestCode.VERIFY_PHONE_NUMBER).setTelNo(amountPhoneNumber).createXml();
        this.sendRequest(UriConstants.URL, checkPhoneNumberXml);
    }

    public void checkIsIVRCard(String phoneNumber, String amountPhoneNumber, Double amount) {
        this.amount = amount;
        this.amountPhoneNumber = amountPhoneNumber;
        this.actionType = CHECK_BIND;
        String postXml = new PostXmlBuilder().setBiPCode(RequestCode.QUERY_PHONE_BIND).setTelNo(phoneNumber).createXml();
        this.sendRequest(UriConstants.URL, postXml);
    }

    @Override
    public void onRequestFinish(String response) {

        if (response == null || "".equals(response)) {
            if (step.equals("0")) {
                AlertDialogUtils.showConfirmDiaLog(paymentPage, response);
                paymentPage.hideProgressDialog();
                ToastUtils.showMessage(paymentPage, "错误，请稍候再试");
            } else if (step.equals("1")) {
                AlertDialogUtils.showConfirmDiaLog(confirmPage, response);
                confirmPage.hideProgressDialog();
                ToastUtils.showMessage(confirmPage, "错误，请稍候再试");
            }
            return;
        }
        if (step.equals("0")) {
            String respCode = SimpleXmlParser.getResponseCode(response);
            if (CHECK_NUMBER.equals(actionType)) {
                if( ResponseCode.NOT_UNICOM_NUMBER.equals(respCode)){
                    paymentPage.hideProgressDialog();
                    ToastUtils.showMessage(paymentPage,"充值失败,充值号码不是联通号码");
                }else{
                    this.checkIsIVRCard(this.payPhoneNumber,this.amountPhoneNumber,this.amount);
                }
            } else {
//            AlertDialogUtils.showConfirmDiaLog(paymentPage,respCode);

                if (!ResponseCode.NOT_BIND_USER.equals(respCode) && !ResponseCode.UNKNOWN_ERROR.equals(respCode)) {//绑定用户
                    paymentPage.hideProgressDialog();
                    String bankName = SimpleXmlParser.getElementsValue(response, "BankName").get(0);
                    String cardNumber = SimpleXmlParser.getElementsValue(response, "BankCard").get(0);
                    Intent intent = new Intent(paymentPage, IvrPasswordInputPage.class);
                    intent.putExtra("amount", this.amount);
                    intent.putExtra("phoneNumber", amountPhoneNumber);
                    intent.putExtra("bankName", bankName);
                    intent.putExtra("cardNumber", cardNumber);
                    paymentPage.startActivity(intent);
                    paymentPage.finish();
                } else if (ResponseCode.NOT_BIND_USER.equals(respCode)) {
                    paymentPage.doUnionPay();
                } else {
                    paymentPage.hideProgressDialog();
                    ToastUtils.showMessage(paymentPage, "银行卡查询是否绑定失败,请重试");
                }
            }
        } else if (step.equals("1")) {
            confirmPage.hideProgressDialog();
            String respCode = SimpleXmlParser.getResponseCode(response);
            if (ResponseCode.SUCCESS.equals(respCode)) {//IVR成功
                Intent intent = new Intent(confirmPage, PaymentSuccessPage.class);
                intent.putExtra("amount", amount);
                intent.putExtra("phoneNumber", amountPhoneNumber);
                confirmPage.startActivity(intent);
                confirmPage.finish();
            } else {
                ToastUtils.showMessage(confirmPage, "错误，支付失败,"+SimpleXmlParser.getResponseDes(response));
            }
        }
    }


    public void payUserIvrInterface(String phoneNumber, String cardPwd, Double amount) {
        String svcCont = "<SvcCont><TelNo>" + phoneNumber + "</TelNo><Type>1</Type>" +
                "<Password>" + cardPwd + "</Password><Amout>" + amount.intValue() + "</Amout></SvcCont>";
        String postXml = new PostXmlBuilder().setBiPCode("100").setTelNo(UserHolder.phoneNumber).setSvcCont(svcCont).createXml();
//        this.phoneNumber = phoneNumber;
//        this.cardNo = cardNo;
//        this.password = cardPwd;
//        this.amount = amount;
//        String postData = buildPostDate();
//        ToastUtils.showMessage(confirmPage, postXml);
        this.amount=amount/100;
        this.amountPhoneNumber=phoneNumber;
        this.actionType = PAY;
        this.sendRequest(UriConstants.URL, postXml);
    }


    @Override
    public void onNetworkAbort() {
        if("0".equals(this.step)){
            paymentPage.hideProgressDialog();
            ToastUtils.showMessage(paymentPage, "错误，网络连接被中断.");
        }else {
            confirmPage.hideProgressDialog();
            ToastUtils.showMessage(confirmPage, "错误，网络连接被中断.");
        }
    }

    @Override
    public void onNetworkError() {
        if("0".equals(this.step)){
            paymentPage.hideProgressDialog();
            ToastUtils.showMessage(paymentPage, "错误，网络连接错误.");
        }else {
            confirmPage.hideProgressDialog();
            ToastUtils.showMessage(confirmPage, "错误，网络连接错误.");
        }

    }

    @Override
    public void onResponseError() {
        if("0".equals(this.step)){
            paymentPage.hideProgressDialog();
            ToastUtils.showMessage(paymentPage, "错误，返回结果错误.");
        }else {
            confirmPage.hideProgressDialog();
            ToastUtils.showMessage(confirmPage, "错误，返回结果错误.");
        }
    }
}
